package com.legal.common.utils;

import org.apache.shiro.codec.Base64;
import org.apache.shiro.crypto.hash.Md5Hash;

/**
 * Shiro加密工具类（对应数据字典中密码加密存储要求）
 * @author Ale
 */
public class ShiroUtils {
    // 加密盐值（需与数据库存储一致）
    private static final String SALT = "legal_aid_salt";
    // 加密迭代次数
    private static final int HASH_ITERATIONS = 1024;

    /**
     * 加密密码
     */
    public static String encryptPassword(String password) {
        return new Md5Hash(password, SALT, HASH_ITERATIONS).toString();
    }

    /**
     * 验证密码
     */
    public static boolean matches(String inputPassword, String dbPassword) {
        String encryptedInput = new Md5Hash(inputPassword, SALT, HASH_ITERATIONS).toString();
        return encryptedInput.equals(dbPassword);
    }

    /**
     * Base64加密
     */
    public static String encryptBase64(String str) {
        return Base64.encodeToString(str.getBytes());
    }

    /**
     * Base64解密
     */
    public static String decryptBase64(String str) {
        return Base64.decodeToString(str.getBytes());
    }
}
